package dao;

import entity.Department;
import entity.School;
import exception.BaseException;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;

public class DepartmentDaoImpl implements DepartmentDaoInterface {
    static private DepartmentDaoInterface departmentDaoImpl = null;
    public DepartmentDaoImpl(){}

    public static DepartmentDaoInterface getInstance(){
        if (departmentDaoImpl == null) {
            departmentDaoImpl = new DepartmentDaoImpl();
            System.out.println("Create DepartmentDaoImpl Object");
        }
        return departmentDaoImpl;
    }
    @Override
    public Department find(Connection conn,Long id) throws SQLException, BaseException {
        //get connection

        //prepare statment
        PreparedStatement pstmt = conn.prepareStatement("SELECT * FROM Department WHERE id = ?");
        pstmt.setLong(1, id);
        //execute query
        ResultSet resultSet = pstmt.executeQuery();
        //analysis result set
        //create Department object(get School object)
        Department desiredDepartment = null;
        if (resultSet.next()) {
            String description = resultSet.getString("description");
            String no = resultSet.getString("no");
            String remarks = resultSet.getString("remarks");
            Long schoolId = resultSet.getLong("school_id");
            School school = SchoolDaoImpl.getInstance().find(schoolId,conn);
            desiredDepartment = new Department(id, description, no, remarks, school);
        }

        return desiredDepartment;
    }

    @Override
    public void add(Department department) throws SQLException {

    }
}
